using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Eve.API
{
    /// <summary>
    /// Specifies the owner of a transaction.
    /// </summary>
    public enum TransactionOwner : int
    {
        /// <summary>
        /// Specifies the transaction's seller.
        /// </summary>
        Seller = 0,
        /// <summary>
        /// Specifies the transaction's buyer.
        /// </summary>
        Buyer = 1,
    }
    /// <summary>
    /// Represents an entry in the wallet journal.
    /// </summary>
    /// <remarks>
    /// <para>The values in the <b>WalletJournalEntry</b> class are similar to the data in the Wallet\Journal view in-game.</para>
    /// <para>Each entry is identified by a reference id that uniquely identifies the transaction, and can be used when walking through
    /// the list of journal entries.</para>
    /// </remarks>
    [Serializable]
    public class WalletJournalEntry
    {
        private DateTime date;
        private int refId;
        private int refTypeId;
        private NameId[] owners = new NameId[2];
        private List<NameId> arguments = new List<NameId>();
        private double amount;
        private double balance;
        private string reason;

        /// <summary>
        /// Gets the date and time at which the transaction occurred.
        /// </summary>
        /// <value>
        /// The Eve date and time at which the transaction occurred.
        /// </value>
        /// <remarks>
        /// <note type="note">This time is not localized.  It represents game time.</note>
        /// </remarks>
        public DateTime DateTime
        {
            get { return date; }
        }
        /// <summary>
        /// Gets the reference identifier for the transaction.
        /// </summary>
        /// <value>
        /// The reference identifier for the transaction.
        /// </value>
        public int RefId
        {
            get { return refId; }
        }
        /// <summary>
        /// Gets the reference type identifier for the transaction.
        /// </summary>
        /// <value>
        /// The reference type identifier for the transaction.
        /// </value>
        /// <remarks>
        /// The value of this property will reference one of the types returned by the <see cref="Eve.API.EveAPI.GetRefTypes">EveAPI.GetRefTypes</see> method.
        /// </remarks>
        public int RefTypeId
        {
            get { return refTypeId; }
        }
        /// <summary>
        /// Gets the name and identifier for the specified owner of the transaction.
        /// </summary>
        /// <param name="index">The owner for which the name and identifier are requested.</param>
        /// <returns>A <see cref="Eve.API.NameId">NameId</see> structure containing the name and Eve internal identifier of the owner specified by <paramref name="index"/>.</returns>
        /// <remarks>
        /// Each transaction has two owners, a seller and a buyer.  Use this method to retrieve information about both owners of the transaction.
        /// </remarks>
        public NameId Owner(TransactionOwner index)
        {
            return owners[(int)index];
        }
        /// <summary>
        /// Gets a list of arguments which are attached to the transaction.
        /// </summary>
        /// <value>
        /// A list of name identifier pairs describing each argument.
        /// </value>
        public List<NameId> Arguments
        {
            get { return arguments; }
        }
        /// <summary>
        /// Gets the amount of the transaction.
        /// </summary>
        /// <value>
        /// The amount of the transaction.
        /// </value>
        /// <remarks>
        /// The the character or corporation whose wallet is being queried is the buying owner of the transaction then the amount will be a negative value, otherwise it will be positive.
        /// </remarks>
        public double Amount
        {
            get { return amount; }
        }
        /// <summary>
        /// Gets the balance of the wallet after the transaction was completed.
        /// </summary>
        /// <value>
        /// The balance of the wallet after the transaction was completed.
        /// </value>
        public double Balance
        {
            get { return balance; }
        }
        /// <summary>
        /// Gets the reason for the transaction.
        /// </summary>
        /// <value>
        /// The reason for the transaction.
        /// </value>
        public string Reason
        {
            get { return reason; }
        }

        internal WalletJournalEntry(XmlElement wj)
        {
            date = DateTime.Parse(wj.Attributes["date"].Value);
            refId = int.Parse(wj.Attributes["refID"].Value);
            refTypeId = int.Parse(wj.Attributes["refTypeID"].Value);
            owners[0].Name = wj.Attributes["ownerName1"].Value;
            owners[0].Id = int.Parse(wj.Attributes["ownerID1"].Value);
            owners[1].Name = wj.Attributes["ownerName2"].Value;
            owners[1].Id = int.Parse(wj.Attributes["ownerID2"].Value);
            int argId = 0;
            while (wj.Attributes["argName" + (++argId)] != null)
            {
                NameId arg = new NameId();
                arg.Name = wj.Attributes["argName" + argId].Value;
                arg.Id = int.Parse(wj.Attributes["argID" + argId].Value);
                arguments.Add(arg);
            }
            amount = double.Parse(wj.Attributes["amount"].Value);
            balance = double.Parse(wj.Attributes["balance"].Value);
            reason = wj.Attributes["reason"].Value;
        }
    }
}
